﻿using System;
using GeometryServer.Extensions;
using Nancy;

namespace GeometryServer.Modules
{
    public class ProjectModule : NancyModule
    {
        public ProjectModule()
            : base("/rest/services/Geometry/GeometryServer/")
        {
            Get["/project"] = parameters =>
            {
                var model = new Models.Project();                

                var exception = new GISServer.Core.Exception();
                exception.Error = new GISServer.Core.Error
                {
                    Code = 400,
                    Message = "Unable to complete Project operation."
                };

                #region Parameter

                if (Request.Query["inSR"].Value != null)
                {
                    int value;
                    var successful = int.TryParse(Request.Query["inSR"].Value, out value);
                    if (successful)
                    {
                        model.InputSpatialReference = value;
                    }
                    else
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("'inSR' parameter is invalid");
                    }
                }

                if (Request.Query["outSR"].Value != null)
                {
                    int value;
                    var successful = int.TryParse(Request.Query["outSR"].Value, out value);
                    if (successful)
                    {
                        model.OutputSpatialReference = value;
                    }
                    else
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("'outSR' parameter is invalid");
                    }

                }


                if (Request.Query["geometries"].Value != null)
                {
                    model.InputGeometries = Request.Query["geometries"].Value;
                }

                if (Request.Query["f"].Value == "json")
                {
                    model.Format = Request.Query["f"].Value;
                }
                else
                {
                    model.Format = "HTML";
                }
                #endregion

                var input_geom_array = new GISServer.Core.Client.Geometry.GeometryArray();

                if ((model.InputGeometries != null) && (!model.HasException))
                {
                    try
                    {
                        input_geom_array = GISServer.Core.Client.Utilities.Parser.GetGeometies(model.InputGeometries);
                        input_geom_array.Project(model.InputSpatialReference, model.OutputSpatialReference);
                    }
                    catch (Exception)
                    {
                        if (!model.HasException)
                        {
                            model.HasException = true;
                        }
                        exception.Error.Details.Add("Unable to project the geometry");
                    }
                }



                if (!model.HasException)
                {
                    model.Result = input_geom_array.ToJson();
                }
                else
                {
                    model.Exception = exception.ToJson();
                }


                if (model.Format.Equals("json"))
                {
                    if (!model.HasException)
                    {
                        return Services.Utilities.GetJsonResponseObject(model.Result);
                    }
                    else
                    {
                        return Services.Utilities.GetJsonResponseObject(model.Exception);
                    }

                }
                else
                {
                    return View["Project", model];
                }
            };

        }
    }
}